package com.portal.framework.util;

import org.springframework.util.ObjectUtils;

public abstract class MatchUtil {

	/**
	 * <p>
	 * [*BO] [B*O] [BO*]
	 * <p>
	 * 
	 * @param pattern
	 * @param str
	 * @return
	 */
	public static boolean simpleMatch(String pattern, String str) {
		if (ObjectUtils.nullSafeEquals(pattern, str) || "*".equals(pattern)) {
			return true;
		}
		if (pattern == null || str == null) {
			return false;
		}
		if (pattern.startsWith("*")
				&& pattern.endsWith("*")
				&& str.indexOf(pattern.substring(1, pattern.length() - 1)) != -1) {
			return true;
		}
		if (pattern.startsWith("*")
				&& str.endsWith(pattern.substring(1, pattern.length()))) {
			return true;
		}
		if (pattern.endsWith("*")
				&& str.startsWith(pattern.substring(0, pattern.length() - 1))) {
			return true;
		}
		return false;
	}

	public static boolean simpleMatch(String[] patterns, String str) {
		if (patterns != null) {
			for (int i = 0; i < patterns.length; i++) {
				if (simpleMatch(patterns[i], str)) {
					return true;
				}
			}
		}
		return false;
	}

}
